package com.ecloud.gardensterminal.server.mqtt;

import com.alibaba.fastjson.JSON;
import com.ecloud.gardensterminal.common.JsonResult;
import com.ecloud.gardensterminal.common.ResultEnum;
import com.ecloud.gardensterminal.common.exception.InnerException;
import com.ecloud.gardensterminal.common.exception.ServerException;
import com.ecloud.gardensterminal.common.util.HttpClient;
import com.ecloud.gardensterminal.constant.MqttTopic;
import com.ecloud.gardensterminal.controller.dto.GardensMqttCallback;
import com.ecloud.gardensterminal.domain.SwitchOrder;
import com.ecloud.gardensterminal.mqtt.MqttMessageFactory;
import com.ecloud.gardensterminal.mqtt.MqttTemplate;
import com.ecloud.gardensterminal.server.MqttService;
import com.ecloud.gardensterminal.utils.MacAddressBase;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

/**
 * @author Squbi
 */
@Service
public class MqttServiceImpl implements MqttService {
    @Autowired
    private MqttTemplate template;

    @Autowired
    private HttpClient httpClient;

    @Value("${server.port}")
    private String port;

    private final MqttMessageFactory messageFactory = new MqttMessageFactory();

    public void publishLocal(byte[] payload) {
        try {
            MqttMessage message = new MqttMessage(payload);
            message.setQos(2);
            template.publish(MacAddressBase.getLocal(), message);
        } catch (MqttException e) {
            throw new InnerException("mqtt publish exception : " + e);
        }
    }

    @Override
    public void sendSwitch(int deviceId, int operation) {
        SwitchOrder switchOrder = new SwitchOrder();
        switchOrder.setOperation(operation);
        switchOrder.setId(deviceId);
        publishLocal(JSON.toJSONString(switchOrder).getBytes());
    }

    @Override
    public void callback(GardensMqttCallback callback) {
        String type = callback.getType();
        HttpMethod httpMethod;
        if ("POST".equals(type)) {
            httpMethod = HttpMethod.POST;
        } else if ("PUT".equals(type)) {
            httpMethod = HttpMethod.PUT;
        } else if ("DELETE".equals(type)) {
            httpMethod = HttpMethod.DELETE;
        } else {
            httpMethod = HttpMethod.GET;
        }
        String result = httpClient.request("http://127.0.0.1:" + port + callback.getUrl(), httpMethod, callback.getData());
        JsonResult jsonResult = JSON.parseObject(result, JsonResult.class);
        jsonResult.setUuid(callback.getUuid());
        try {
            template.publish(MqttTopic.CALLBACK_TOPIC, messageFactory.createMessage(JSON.toJSONString(jsonResult).getBytes()));
        } catch (MqttException e) {
            throw new ServerException(ResultEnum.MQTT_BAD_CALLBACK, e.getMessage());
        }

    }
}
